#kernel入口
ENTRYPOINT = 0x30400

# 编译程序参数
ASM = nasm
CC = gcc
LD = ld
ASMBFLAGS = -I boot/include/
ASMKFLAGS = -I include/ -f elf
CFLAGS	 = -I include/ -c -m32
LDFLAGS = -m elf_i386 -s -Ttext $(ENTRYPOINT)

TAR_DIR = build
OBJS = $(TAR_DIR)/kernel.o $(TAR_DIR)/start.o
ORANGESKERNEL	= kernel.bin
#nasm -o loader.bin loader.asm	
#sudo cp loader.bin /mnt/floppy
test_loader:
	nasm -o loader.bin loader.asm	
	nasm -f elf -o kernel.o kernel.asm
	gcc -c -o start.o start.c -m32
	ld -s -m elf_i386 -Ttext 0x30400 -o kernel.bin kernel.o start.o
	sudo mount a.img /mnt/floppy
	sudo cp kernel.bin /mnt/floppy
	sudo cp loader.bin /mnt/floppy
	sudo umount /mnt/floppy 
	
test_c:
	nasm -f elf -o kernel.o kernel.asm
	gcc -c -o start.o start.c -m32
	ld -s -m elf_i386 -Ttext 0x30400 -o kernel.bin kernel.o start.o
	sudo mount a.img /mnt/floppy
	sudo cp kernel.bin /mnt/floppy
	sudo cp loader1.bin /mnt/floppy/loader.bin
	sudo umount /mnt/floppy 
build:$(TAR_DIR)/$(ORANGESKERNEL) $(TAR_DIR)/loader.bin mbr
	sudo mount a.img /mnt/floppy
	sudo cp $(TAR_DIR)/$(ORANGESKERNEL) /mnt/floppy
	sudo cp $(TAR_DIR)/loader.bin /mnt/floppy
	sudo umount /mnt/floppy 
	bochs
$(TAR_DIR)/$(ORANGESKERNEL):$(OBJS)
	$(LD) $(LDFLAGS) -o $@ $^
$(TAR_DIR)/boot.bin:boot/boot.asm builddir
	$(ASM) $(ASMBFLAGS) $< -o $@
$(TAR_DIR)/loader.bin:boot/loader.asm builddir
	$(ASM) $(ASMBFLAGS) $< -o $@

mbr:$(TAR_DIR)/boot.bin builddir
	dd if=$(TAR_DIR)/boot.bin of=a.img bs=512 count=1 conv=notrunc

$(TAR_DIR)/kernel.o:kernel/kernel.asm builddir
	$(ASM) $(ASMKFLAGS) $< -o $@
$(TAR_DIR)/start.o:kernel/start.c builddir
	$(CC) $(CFLAGS) $< -o $@
	
builddir:
	mkdir $(TAR_DIR) -p
	
clean:
	rm -rf $(TAR_DIR)
